package com.qiqu.controller;

import com.qiqu.commen.Const;
import com.qiqu.entity.Enclosure;
import com.qiqu.entity.Users;
import com.qiqu.service.EnclosureService;
import com.qiqu.utils.FetchVideoCover;
import com.qiqu.utils.JSONResult;
import io.swagger.annotations.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("enclosure")
@Api(value = "附件相关接口",tags = {"附件相关接口controller"})
public class EnclosureController {
    @Resource
    private EnclosureService enclosureService;

    //上传图片
    @ApiOperation(value = "图片上传--fk",notes = "图片上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value = "用户Id",required = true, dataType = "string",paramType = "form"),
            @ApiImplicitParam(name="articleId",value = "文章Id",required = true, dataType = "string",paramType = "form")

    })
    @PostMapping(value = {"/uploadFace"},headers = "content-type=multipart/form-data")
    public JSONResult uploadFace(String userId,String articleId,
                                 @ApiParam(value = "用户头像",required = true)
                                         MultipartFile file) throws IOException {
        if(StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("用户Id不能为空...");
        }

        //文件保存路径
        String fileSpace= Const.FILE_SPACE;
        //数据库保存路径
        String uploadPathDB = "/rbook/"+userId+"/face";
        FileOutputStream fileOutputStream =null;
        InputStream inputStream=null;
        try{
            if(file !=null){
                //获取文件名
                String fileName=file.getOriginalFilename();
                if(StringUtils.isNotBlank(fileName)){
                    //文件上传的最终路径
                    String finalFacePath = fileSpace + uploadPathDB +"/"+fileName;
                    //设置数据库保存路径
                    uploadPathDB +="/"+fileName;
                    File outFile = new File(finalFacePath);
                    if(outFile.getParentFile()!=null || outFile.getParentFile().isDirectory()){
                        //创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }
                    fileOutputStream = new FileOutputStream(outFile);
                    inputStream = file.getInputStream();
                    IOUtils.copy(inputStream,fileOutputStream);
                }
            }else {
                return JSONResult.errorMsg("上传出错...");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.errorMsg("上传出错...");
        }finally {
            if(fileOutputStream !=null){
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        }
        Enclosure enclosure=new Enclosure();
        enclosure.setArticle(articleId);
        System.out.println(articleId);
        enclosure.setUserId(userId);
        enclosure.setPath(uploadPathDB);
        enclosure.setHeight(0);
        enclosure.setWidth(0);
        enclosure.setType(Const.Role.photo.code);
        enclosure.setCreateTime(new Date());
        enclosureService.addEnclosure(enclosure);
        return JSONResult.ok(uploadPathDB);
    }


}